﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace JQueryWidget.WidgetFrame.Common
{
    public class WidgetStoreTemplate:System.Web.UI.UserControl,IWidgetStoreTemplate
    {
        public WidgetStoreTemplate()
            : base()
        {
            NeedWrapper = true;
        }

        public int TemplateDataID
        {
            get;
            set;
        }

        public bool NeedWrapper
        {
            get;
            set;
        }

        public virtual void LoadColumns()
        {
            if (TemplateDataID > 0)
            {
                var templateData = (from td in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                    where td.Element("id").Value == TemplateDataID.ToString()
                                    select td).FirstOrDefault();
                if (templateData != null)
                {
                    var columns = (from c in templateData.Element("columns").Elements("column")
                                   select c).ToList();
                    foreach (var column in columns)
                    {
                        var columnId = column.Attribute("id").Value; 
                        var columnControl = FindControl(columnId) as ITemplateColumn;
                        columnControl.ColumnID = columnId;
                        columnControl.NeedWrapper = NeedWrapper;
                        columnControl.TemplateDataID = TemplateDataID;
                        columnControl.LoadWidgets();
                    }
                }
            }
        }
         

    }
}